﻿using EventBus.Abstractions;
using EventBus.Extensions;
using MediatR;
using Microsoft.Extensions.Logging;
using Ordering.API.Application.Commands;
using Ordering.API.Application.IntegrationEvents.Events;
using Ordering.Domain.AggregatesModel.OrderAggregate;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ordering.API.Application.IntegrationEvents.EventHandling
{
    public class OrderPaymentSucceededIntegrationEventHandler :
         IIntegrationEventHandler<OrderPaymentSucceededIntegrationEvent>
    {
        private readonly IMediator _mediator;
        private readonly ILogger<OrderPaymentSucceededIntegrationEventHandler> _logger;
        private readonly IOrderRepository _orderRepository;
        public OrderPaymentSucceededIntegrationEventHandler(IMediator mediator,
            ILogger<OrderPaymentSucceededIntegrationEventHandler> logger,
            IOrderRepository orderRepository)
        {
            _mediator = mediator;
            _logger = logger;
            _orderRepository = orderRepository;
        }
        public async Task Handle(OrderPaymentSucceededIntegrationEvent @event)
        {
            var command = new SetPaidOrderStatusCommand(@event.OrderIds);
            _logger.LogInformation(
                    "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
                    command.GetGenericTypeName(),
                    nameof(command.OrderIds),
                    command.OrderIds,
                    command);
            await _mediator.Send(command);

        }
    }
}
